﻿using System.Linq;
using System.Web.Mvc;
using Orchard;
using Orchard.Localization;
using Orchard.Environment.Extensions;
using FruitCake.ContactPage.Services;
using Orchard.UI.Navigation;
using Orchard.Settings;
using Orchard.DisplayManagement.Shapes;
using FruitCake.ContactPage.Models;
using System.Collections.Generic;
using Orchard.DisplayManagement;
using Orchard.Core.Contents.Controllers;
using FruitCake.ContactPage.ViewModels;
using Orchard.Mvc.Extensions;

namespace FruitCake.ContactPage.Controllers
{
    [OrchardFeature("FruitCake.ContactPage")]
    public class AdminController : Controller
    {
        private readonly IOrchardServices _services;
        private readonly ISiteService _siteService;
        private readonly IContactService _contactService;

        public AdminController(
            IOrchardServices orchardServices, 
            ISiteService siteService, 
            IShapeFactory shapeFactory,
            IContactService contactService)
        {
            _services = orchardServices;
            _siteService = siteService;
            Shape = shapeFactory;
            _contactService = contactService;
        }

        dynamic Shape { get; set; }
        public Localizer T { get; set; }

        public ActionResult Index(PagerParameters pagerParameters)
        {
            if (!_services.Authorizer.Authorize(Permissions.ManageContactRequests, T("Not allowed to manager request")))
                return new HttpUnauthorizedResult();

            Pager pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);
            
            IEnumerable<ContactRequestRecord> requests = _contactService.GetRequests(pager.Page, pager.PageSize);

            var pagerShape = Shape.Pager(pager).TotalItemCount(_contactService.GetRequestCount());
          
            return View(new ViewModels.ContactRequestsViewModel
                {
                    Requests = requests,
                    PagerShape = pagerShape
                });
        }

        [HttpPost, ActionName("Index")]
        [FormValueRequired("submit.BulkEdit")]
        public ActionResult IndexPOST(RequestListOptions options, IEnumerable<int> itemIds, string returnUrl)
        {
            if (!_services.Authorizer.Authorize(Permissions.ManageContactRequests, T("Not allowed to manager request")))
                return new HttpUnauthorizedResult();

            if (itemIds != null)
            {
                _contactService.DoRequestAction(options.BulkAction, itemIds);
            }

            return this.RedirectLocal(returnUrl, () => RedirectToAction("Index"));
        }

        public ActionResult Details(int id)
        {
            if (!_services.Authorizer.Authorize(Permissions.ManageContactRequests, T("Not allowed to manager request")))
                return new HttpUnauthorizedResult();

            return View(_contactService.GetRequest(id));
        }

        [HttpPost, ActionName("Details")]
        [FormValueRequired("submit.DetailEdit")]
        public ActionResult DetailsPOST(int id)
        {

            return RedirectToAction("Details", new { id = id });
        }

        public ActionResult Edit(int id)
        {
            if (!_services.Authorizer.Authorize(Permissions.ManageContactRequests, T("Not allowed to manager request")))
                return new HttpUnauthorizedResult();

            return View(_contactService.GetRequest(id));
        }

        public ActionResult Delete(int id)
        {
            if (!_services.Authorizer.Authorize(Permissions.ManageContactRequests, T("Not allowed to manager request")))
                return new HttpUnauthorizedResult();

            return View();
        }        
    }
}